import isolines as isolns
import FractCalc

def density_grid(data, size_of_gridwind, size_of_averwind):
    # prepare a blank mesh
    mesh=[]
    for ddir in range(0,360,size_of_gridwind):
        tmp=[]
        for ddip in range(0,180,size_of_gridwind):        
            tmp.append(0)            
        mesh.append(tmp)            
    
    for ddir in range(360/size_of_gridwind):
        for ddip in range(180/size_of_gridwind):
            for i in data:
                if abs(i[0]-ddir*size_of_gridwind)<size_of_averwind and \
                            abs(i[1]-ddip*size_of_gridwind)<size_of_averwind:
                    mesh[ddir][ddip]+=1
    return mesh
        
          

def double_data(data):
    result=[]
    for i in data:
        j1=i[0],i[1]
        if i[0]<180:
            j2=i[0]+180,180-i[1]
        else:
            j2=i[0]-180,180-i[1]
        result.append(j1)
        result.append(j2)
    return result  

def double_doubled_data(data):
    result = []
    for i in data:
        j1=i[0],i[1]
        if i[0]<180:
            j2=i[0]+180,180-i[1]
        else:
            j2=i[0]-180,180-i[1]
        j3=i[0],(0-i[1])
        j4=j2[0],180+i[1]
        
        j5=(0-j1[0]),j1[1]
        j6=(0-j3[0]),j3[1]
        j7=(0-j2[0]),j2[1]
        j8=(0-j4[0]),j4[1]
        
        j9=360+j3[0],j3[1]
        j10=360+j1[0],j1[1]
        j11=360+j2[0],j2[1]
        j12=360+j4[0],j4[1]
        
        result.append(j1)
        result.append(j2)
        result.append(j3)
        result.append(j4)
        result.append(j5)
        result.append(j6)
        result.append(j7)
        result.append(j8)
        result.append(j9)
        result.append(j10)
        result.append(j11)
        result.append(j12)
    return result
    

def isolines(data, intervals_step, intervals_limit):
    data=[data[0]]+data+[data[-1]]
    tmp=[]
    for i in data:
        j=[i[-1]]+i+[i[0]]
        tmp.append(j)
    intervals=range(0,intervals_limit,intervals_step)
    prepared_matrix=isolns.prepare_matrix(tmp)
    calculated_dots=isolns.calculate_dots(prepared_matrix,intervals)
    coords=isolns.calculate_coords(calculated_dots)
    curves=isolns.collect_lines(coords)
    result=[]
    for curve in curves:
        result.append([curve[0]])
        for xy in curve[1]:
            result[-1].append(xy[0])
            result[-1].append(xy[1])        
    return result

def local_max(data, size_of_gridwind):
    lmax=[]
    for y in range(len(data)):
        for x in range(len(data[y])):
            if data[y][x]==0: continue
            neig = [data[y_][x_] for y_ in range(len(data)) if abs(y-y_)<2 \
                              for x_ in range(len(data[y])) if abs(x-x_)<2 ]
            quest = [data[y][x]] * len(neig)
            answers = map(max, quest, neig)

            if answers==quest:
                lmax.append([y*size_of_gridwind+int(0.5*size_of_gridwind),\
                              x*size_of_gridwind+int(0.5*size_of_gridwind),\
                              data[y][x]])
    return lmax

def angels_between(data):
    result = []
    a = FractCalc.Fracture()
    b = FractCalc.Fracture()
    for i in data:
        for j in data:
            a.dir = i[0]; a.dip = i[1]
            b.dir = i[0]; b.dip = i[1]
            result.append([i, j, FractCalc.Angel(a,b)])
    return result
